import java.util.PriorityQueue;
import java.util.Queue;


public class Supervisor {
	private int active;
	private int idle;
	private int minidle;
	private int maxidle;
	private int n;
	private Queue<Job> queue;
	
	public Supervisor(int minidle, int maxidle, int n){
		this.minidle = minidle;
		this.maxidle = maxidle;
		this.n = n;
		active = 0;
		queue = new PriorityQueue<Job>();
	}
	public void begin(){
		idle--;
		active++;
		if (idle < minidle){
			for(int i = 0; i < (maxidle - minidle)/2; i++){
				new PoolThread(this);
				idle++;
			}
		}
	}
	public void end(){
		idle++;
		active--;
	}
	public boolean canContinue(){
		return idle <= maxidle;
	}
	public void start() {
		for(int i = 0; i < minidle; i++){
			new PoolThread(this).start();
			idle++;
		}
	}
	public Job get(){
		synchronized (queue) {
			if (queue.size() == 0){
				try {
					System.out.println("get wait");
					queue.wait();
					System.out.println("get reprend");
				} catch (InterruptedException e) {

					e.printStackTrace();
				}
			}

			return queue.poll();
		}
	}
	public void put(Job job){
		synchronized (queue) {
			queue.add(job);
			queue.notify();
		}
	}
}
